package cn.gov.sdcj.accountancy.rule

import cn.gov.sdcj.accountancy.entity.VoucherList
import org.junit.Assert
import org.junit.Before
import org.junit.Test

/**
 * <p>Description: 
 * </p>
 *
 * Created by laijie on 2015/3/27.
 */
class RuleManagerTest {
    List pzs;
    private File testFile;

    @Before
    public void setUp() {
        pzs = [
                [id: 1, bdgtType: '2', bdgtId: '2011701', bdgtName: '一般公共服务支出--质量技术监督与检验检疫事务--行政运行'
                 , amt: 1234567.89, payType : '01', payTypeName: '直接支付'
                 , unitId: '0011', unitName: 'AAA单位', projectId: '00110001', projectName: '测试项目1'
                 , srcId: '064', srcName: '暂存款', fexp: '测试用的1'],
                [id: 22, bdgtType: '4', bdgtId: '2011702', bdgtName: '一般公共服务支出--质量技术监督与检验检疫事务--一般行政管理事务'
                 , amt: 12345.67, payType : '01', payTypeName: '直接支付'
                 , unitId: '0012', unitName: 'BBB单位', projectId: '00110002', projectName: '测试项目2'
                 , srcId: '100', srcName: '预算内', fexp: '测试用的2'],
                [id: 33, bdgtType: '2', bdgtId: '2011703', bdgtName: '一般公共服务支出--质量技术监督与检验检疫事务--机关服务'
                 , amt: 123.45, payType : '01', payTypeName: '授权支付'
                 , unitId: '0013', unitName: 'CCC单位', projectId: '00110003', projectName: '测试项目3'
                 , srcId: '051', srcName: '不知道', fexp: '测试用的3']
        ]
        URI file = this.class.getResource("/testRules.groovy").toURI();
        testFile = new File(file);
        ScriptLoader.load(testFile);
    }

    @Test
    public void testApply() {
        VoucherList aT;
        aT = RuleManager.apply("T1", pzs, [name: "test"], ['date': new Date()]);
        Assert.assertEquals(aT.size(), 2);
        Assert.assertEquals(aT.getVoucher(0).preparerName, "test");
        Assert.assertEquals(aT.getVoucher(1).groupName, "收");
        Assert.assertEquals(aT.getVoucher(1).entries[0].accountNum, "501.201.17.01.01.14");
        System.out.print(aT);

        System.out.print("=============for each================");
        pzs.each { pz ->
            aT = RuleManager.apply("T2", pz, [name: "test each"], ['date': new Date()]);
            Assert.assertEquals(aT.size(), 1);
            Assert.assertEquals(aT.getVoucher(0).preparerName, "test each");
            Assert.assertNull(aT.getVoucher(0).groupName);
            Assert.assertEquals(aT.getVoucher(0).entries[0].accountNum, "121.02");
            System.out.print(aT);
        }
    }
}
